//home模块小仓库
import { reqMyOrderList, reqAddCollect, reqMyCollectList, reqDeleteCollect, reqCartToCollect, reqDeleteOrder, 
    reqMyHistoryOrderList, reqSuccessHistoryOrder,reqAddAfterSales ,reqMyAfterSalesList} from "@/api";
const state = {
    orderList: {},
    historyOrderList: {},
    collectList: {},
    successHistoryOrder: [],
    afterSalesList:{},
};
const mutations = {
    GETORDERLIST(state, orderList) {
        state.orderList = orderList;
    },
    GETHISTORYORDERLIST(state, historyOrderList) {
        state.historyOrderList = historyOrderList;
    },
    GETCOLLECTLIST(state, collectList) {
        state.collectList = collectList;
    },
    GETSUCCESSHISTORYORDER(state, successHistoryOrder){
        state.successHistoryOrder=successHistoryOrder;
    },
    GETAFTERSALESLIST(state,afterSalesList){
        state.afterSalesList=afterSalesList;
    }
};
const actions = {
    //获取订单数据
    async getOrderList({ commit }, { page, limit }) {
        let result = await reqMyOrderList(page, limit);
        if (result.code == 200) {
            commit("GETORDERLIST", result.data)
        }
    },
    //获取历史记录数据
    async getHistoryOrderList({ commit }, { page, limit }) {
        let result = await reqMyHistoryOrderList(page, limit);
        if (result.code == 200) {
            commit("GETHISTORYORDERLIST", result.data)
        }
    },
    //取消订单
    async deleteOrder({ commit }, orderId) {
        let result = await reqDeleteOrder(orderId);
        if (result.code == 200) {
            return 'ok';
        } else {
            //当前函数如果执行返回Promise
            return Promise.reject(new Error('false'));
        }
    },
    //增加一个商品到收藏
    async addCollect({ commit }, { waresId }) {
        let result = await reqAddCollect(waresId);
        if (result.code == 200) {
            return 'ok';
        } else {
            //当前函数如果执行返回Promise
            return Promise.reject(new Error('false'));
        }
    },
    //获取收藏数据
    async getCollectList({ commit }, { page, limit }) {
        let result = await reqMyCollectList(page, limit);
        if (result.code == 200) {
            commit("GETCOLLECTLIST", result.data)
        }
    },
    //删除收藏中商品
    async deleteCollectById({ commit }, collectId) {
        let result = await reqDeleteCollect(collectId);
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //提交购物车商品到收藏
    async insertCartToCollect({ commit }) {
        let result = await reqCartToCollect();
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //获取成功的历史记录
    async getSuccessHistoryOrder({ commit }) {
        let result = await reqSuccessHistoryOrder();
        if (result.code == 200) {
            commit("GETSUCCESSHISTORYORDER", result.data)
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //提交售后申请
    async addAfterSales({ commit }, data){
        let result = await reqAddAfterSales(data);
        if(result.code == 200){
            alert(result.data);
            return 'ok';
        }else{
            return Promise.reject(new Error('faile'));
        }
    },
    //获取售后记录数据
    async getAfterSalesList({ commit }, { page, limit }) {
        let result = await reqMyAfterSalesList(page, limit);
        console.log('result',result.data);
        if (result.code == 200) {
            commit("GETAFTERSALESLIST", result.data)
        }
    },

};
const getters = {};
export default {
    state,
    mutations,
    actions,
    getters
}